<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;

/**
 * 订单控制器
 */
class OrderController extends HomeController {


	public function orderlist(){
	    $uid=is_login();
        $order_list1=M('order_list')->where(array('user_id'=>$uid,'status'=>1))->select();
        $order_list2=M('order_list')->where(array('user_id'=>$uid,'status'=>2))->select();
        $order_list3=M('order_list')->where(array('user_id'=>$uid,'status'=>3))->select();
        $this->assign('order_list1',$order_list1);
        $this->assign('order_list2', $order_list2);
        $this->assign('order_list3', $order_list3);
		$this->display();
	}

    //搜索订单
    public function ajaxPostOrderList(){
        $data=I('post.');
        $status=I('get.status');
        $where['status']=$status;
        if(!empty($data['order_sn'])){
            $where['order_sn']=$data['order_sn'];
            $list=M('order_list')->where($where)->select();
        }else {
            if (!empty($data['start_time'])) {
                $where['create_time']  = array('gt', strtotime($data['start_time']));
            }
            if (!empty($data['end_time'])) {
                $where['create_time'] = array('lt',strtotime($data['end_time']));
            }

            if(!empty($data['start_time']) && !empty($data['end_time'])){
                $where['create_time'] = array('between',array(strtotime($data['start_time']),strtotime($data['end_time'])));
            }
            (!empty($data['bid_sn'])) ? $where['bid_sn'] = $data['bid_sn'] : '';

            $where['user_id']=is_login();
            $list=M('order_list')->where($where)->select();
        }

        switch ($status){
            case 1:
                $tpl='ajax_list1';
                break;
            case 2:
                $tpl='ajax_list2';
                break;
            case 3:
                $tpl='ajax_list3';
                break;
        }
        $this->assign('list', $list);
        $html=$this->fetch($tpl);
        $this->ajaxReturn($html);
    }


    //修改订单状态
    public function setOrderStatus(){
        $where['id']=I('get.id');
        if( M('order_list')->where($where)->setField('status',I('get.status'))) {
            $this->success('操作成功','',true);
        }else{
            $this->error('操作失败！','',true);
        }
    }

    //提交退货单
    public function submitReturnOrder(){
       if(IS_POST){
           $data['return_sn']=getOrderSn();
           $data['order_id']=I('get.order_id');
           $data['user_id']=is_login();
           $data['create_time']=time();
           $data['return_count']=I('return_count');
           $data['return_des']=I('return_des');
           $data['status']=1;
           $re=M('return_list')->add($data);
           if($re!==false){
               $this->success('提交成功',U('returnlist'),true);
           }else{
               $this->error('提交失败！','',true);
           }
       }else{
           $this->assign('url',U('Order/returnlist'));
           $this->display();
       }
    }

    //退货单
    public function returnlist(){
        $uid=is_login();
        $order_list1=M('return_list')->where(array('user_id'=>$uid,'status'=>1))->select();
        $order_list2=M('return_list')->where(array('user_id'=>$uid,'status'=>2))->select();
        $this->assign('order_list1',$order_list1);
        $this->assign('order_list2', $order_list2);
        $this->display();
    }

    //搜索退货单
    public function ajaxPostReturnlist(){
        $data=I('post.');
        $status=I('get.status');
        $where['status']=$status;
        if(!empty($data['return_sn'])){
            $where['return_sn']=$data['return_sn'];
            $list=M('return_list')->where($where)->select();
        }else {
            if (!empty($data['start_time'])) {
                $where['create_time']  = array('gt', strtotime($data['start_time']));
            }
            if (!empty($data['end_time'])) {
                $where['create_time'] = array('lt',strtotime($data['end_time']));
            }
            $list=M('return_list')->where($where)->select();
        }
        switch ($status){
            case 1:
                $tpl='return_list1';
                break;
            case 2:
                $tpl='return_list2';
                break;
        }
        $this->assign('list', $list);
        $html=$this->fetch($tpl);
        $this->ajaxReturn($html);
    }

    //查看物品明细
    public function ordergoodinfo(){
       $id=I('get.order_id');
       $order_info=M('order_list')->where(array('id'=>$id))->find();
       $bid_user=M('bid_user')->where(array('bid_id'=>$order_info['bid_id'],'user_id'=>$order_info['user_id'],'good_id'=>$order_info['good_id']))->find();
       switch ($order_info['bid_cate']) {
            case '1':
                $good_list=M('bid_good')->where(array('bid_id'=>$order_info['bid_id']))->select();
                break;
            case '2':
                $good_list=M('bid_good')->where(array('bid_id'=>$order_info['bid_id'],'id'=>$order_info['good_id']))->select();
                break;
      }
      $this->assign('bid_user',$bid_user);
      $this->assign('good_list',$good_list);
      $this->assign('bid_cate',$order_info['bid_cate']);
      $this->display();
    }



}
